﻿using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Service.Domain.Interfaces;
using Service.Domain.Interfaces.Repositories;
using Service.Repository.User;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Service.Repository
{
    public static class RepositoryServiceExtensions
    {
        public static void AddRepository(this IServiceCollection service, Action<RepositoryOption> action = null)
        {
            service.AddSingleton((provider) =>
            {
                var option = new RepositoryOption()
                {
                    ConnectionStringName = "DefaultConnectionString",
                };

                if (action != null)
                {
                    action(option);
                }

                var connStr = provider.GetRequiredService<IConfiguration>().GetConnectionString(option.ConnectionStringName);

                return new DapperConnectionFactory(connStr);
            });
            service.AddTransient<IDbContext, DbContext>();
            service.AddSingleton<IUserRepository, UserRepository>();
            service.AddSingleton<IPermissionRepository, PermissionRepository>();
            service.AddSingleton<IRoleRepository, RoleRepository>();
        }
    }
}
